package com.panopset.gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import com.panopset.Alert;
import com.panopset.Strings;
import com.panopset.Util;
import com.panopset.UtilIO;
import com.panopset.java.Version;
import com.panopset.swing.FieldGui;
import com.panopset.swing.Scaffolding;

/**
 * Panopset list editor.
 * 
 * @author Karl Dinwiddie
 * 
 */
public class PropertiesEditor extends AbstractTableEditor {

	/**
	 * @param defaultResourcePath
	 *            Default resource path.
	 * @param fileName
	 *            File name.
	 */
	public PropertiesEditor(final String defaultResourcePath,
			final String fileName) {
		super(defaultResourcePath, fileName);
		setColumnHeader(Util.capitalize(Util.dropExtension(fileName)));
		setFieldGui(new FieldGui() {

			@Override
			public String getValue() {
				StringWriter sw = new StringWriter();
				DefaultTableModel tm = getModel();
				if (tm == null) {
					return null;
				}
				Vector<?> dv = tm.getDataVector();
				if (dv == null || dv.isEmpty()) {
					return null;
				}
				for (Object o : tm.getDataVector()) {
					if (o != null && o instanceof Vector<?>) {
						Vector<?> v = (Vector<?>) o;
						if (v.size() == 2) {
							sw.append(v.elementAt(0).toString());
							sw.append(UtilIO.MAP_SEP);
							sw.append(v.elementAt(1).toString());
							sw.append(Strings.getEol());
						}
					}
				}
				return sw.toString();
			}

			@Override
			public void setValue(final String value) {
				try {
					getModel().getDataVector().clear();
					StringReader sr = new StringReader(value);
					BufferedReader br = new BufferedReader(sr);
					String s = br.readLine();
					while (s != null) {
						StringTokenizer st = new StringTokenizer(s,
								UtilIO.MAP_SEP);
						if (st.countTokens() == 2) {
							getModel().addRow(
									new String[] { st.nextToken(),
											st.nextToken() });
						}
						s = br.readLine();
					}
					br.close();
				} catch (IOException e) {
					Util.log(e);
				}
			}
		});
	}

	/**
	 * Value column header.
	 */
	private String keyColumnHeader = "Key";

	/**
	 * @return Edited map as Properties.
	 */
	public Properties getProperties() {
		Properties p = new Properties();
		try {
			StringReader sr = new StringReader(getValue());
			BufferedReader br = new BufferedReader(sr);
			String s = br.readLine();
			while (s != null) {
				String[] spl = UtilIO.split(s);
				p.put(spl[0], spl[1]);
				s = br.readLine();
			}
		} catch (IOException e) {
			Util.log(e);
		}
		return p;
	}

	@Override
	protected void doAdd() {
		getModel().addRow(new String[] { "", "" });
	}

	@Override
	protected DefaultTableModel createModel() {
		return new DefaultTableModel(0, 2) {
			@Override
			public String getColumnName(final int columnIndex) {
				switch (columnIndex) {
				case 0:
					return keyColumnHeader;
				case 1:
					return getValueColumnHeader();
				default:
					return "undefined";
				}
			}

			/**
			 * Serial version id.
			 */
			private static final long serialVersionUID = Version.UNI_VERSION;
		};
	}
}
